package com.lubase.wfengine.controller;

import com.lubase.core.config.PassToken;
import com.lubase.core.response.ResponseData;
import com.lubase.orm.exception.ParameterNotFoundException;
import com.lubase.orm.service.AppHolderService;
import com.lubase.orm.util.SpringUtil;
import com.lubase.wfengine.WfengineApplication;
import com.lubase.wfengine.model.ReturnInfoModel;
import com.lubase.wfengine.model.TransferInfoModel;
import com.lubase.wfengine.service.WorkflowMonitorService;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("wftMonitor")
public class WorkFlowMonitorController {

    @Autowired
    WorkflowMonitorService monitorService;

    @Autowired
    AppHolderService holderService;

    /**
     * 流程监控：获取可退回的流程节点
     *
     * @param fInsId
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "getReturnBackTaskList", method = RequestMethod.POST)
    public ResponseData<List<ReturnInfoModel>> getReturnBackTaskList(@NonNull String fInsId) {
        if (StringUtils.isEmpty(fInsId)) {
            throw new ParameterNotFoundException("fInsId");
        }
        return ResponseData.success(monitorService.getReturnBackTaskList(fInsId));
    }

    /**
     * 流程监控：获取待转办人员列表
     *
     * @param fInsId
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "getTransferUserList", method = RequestMethod.POST)
    public ResponseData<List<TransferInfoModel>> getTransferUserList(@NonNull String fInsId) {
        if (StringUtils.isEmpty(fInsId)) {
            throw new ParameterNotFoundException("fInsId");
        }
        return ResponseData.success(monitorService.getCurrentApprovalUser(fInsId));
    }

    /**
     * 流程监控：管理员转办任务
     *
     * @param oInsId
     * @param toUserId
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "transfer", method = RequestMethod.POST)
    public ResponseData<Integer> transfer(@NonNull String oInsId, @NonNull String toUserId) {
        if (StringUtils.isEmpty(oInsId) || StringUtils.isEmpty(toUserId)) {
            throw new ParameterNotFoundException("fInsId or toUserId");
        }
        String currentUserId = holderService.getUser().getId().toString();
        return ResponseData.success(monitorService.transfer(oInsId, currentUserId, toUserId));
    }

    /**
     * 流程监控：流程退回
     *
     * @param fInsId
     * @param taskId
     * @param memo
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "returnBack", method = RequestMethod.POST)
    public ResponseData<Integer> returnBack(@NonNull String fInsId, @NonNull String taskId, String memo) {
        if (StringUtils.isEmpty(fInsId) || StringUtils.isEmpty(taskId)) {
            throw new ParameterNotFoundException("fInsId or taskId");
        }
        String currentUserId = holderService.getUser().getId().toString();
        return ResponseData.success(monitorService.returnBack(fInsId, taskId, memo, currentUserId));
    }

    /**
     * 流程监控：流程放弃
     *
     * @param fInsId
     * @param memo
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "giveUp", method = RequestMethod.POST)
    public ResponseData<Integer> giveUp(@NonNull String fInsId, String memo) {
        if (StringUtils.isEmpty(fInsId)) {
            throw new ParameterNotFoundException("fInsId");
        }
        return ResponseData.success(monitorService.giveUp(holderService.getUser(), fInsId, memo));
    }

    @PassToken
    @RequestMapping(value = "/getAppStartInfo", method = RequestMethod.GET)
    public HashMap<String, String> getAppStartInfo() throws URISyntaxException {
        HashMap<String, String> appStartInfoModel = new HashMap<>();
        appStartInfoModel.put("appStartTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(SpringUtil.getApplicationContext().getStartupDate())));
        File file = new File(WfengineApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        String jarName = "";
        try {
            jarName = file.getParentFile().getParentFile().getName();
            if (jarName.endsWith("!")) {
                jarName = jarName.substring(0, jarName.length() - 1);
            }
        } catch (Exception e) {
        }
        appStartInfoModel.put("jarName", jarName);
        appStartInfoModel.put("jarUpdateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified())));
        return appStartInfoModel;
    }
}
